<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(0);
header('Content-Type: text/html; charset=utf-8');

class Admin_category extends CI_Controller {

    protected $__orderedNumberCateNews;

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->library('session');
        $this->load->model('users_model');
        $this->load->model('admin_category_model');
        $this->load->library("pagination");
    }

    public function getSubCategoryNews()
    {
        $this->session->set_userdata('choseCategoryNews', '');
        $idCateNews = $this->input->post('id');
        if($idCateNews == ''){
            echo "";
        }
        else{
            $this->getIDSubCategoryNews($idCateNews);
            echo $this->session->userdata('choseCategoryNews');
        }
    }

    public function getIDSubCategoryNews($id)
    {
        $idUser = $this->getIDUser();
        $result = $this->admin_category_model->getSubCategoryNews($id, $idUser);
        $category = $result[name]." > ".$this->session->userdata('choseCategoryNews');
        $this->session->set_userdata('choseCategoryNews', $category);
        if($result[parentid] == 0){
            return $category;
        }
        else{
            $this->getIDSubCategoryNews($result[parentid]);
        }
    }

    public function getEditSubCategoryNews()
    {
        $this->session->set_userdata('choseEditCategoryNews', '');
        $idCateNews = $this->input->post('id');
        $this->getIDEditSubCategoryNews($idCateNews);
        echo $this->session->userdata('choseEditCategoryNews');
    }

    public function getIDEditSubCategoryNews($id)
    {
        $idUser = $this->getIDUser();
        $result = $this->admin_category_model->getSubCategoryNews($id, $idUser);
        $category = $result[name]." > ".$this->session->userdata('choseEditCategoryNews');
        $this->session->set_userdata('choseEditCategoryNews', $category);
        if($result[parentid] == 0){
            return $category;
        }
        else{
            $this->getIDEditSubCategoryNews($result[parentid]);
        }
    }

    public function createCategoryNews()
    {
        $idUser = $this->getIDUser();
        $name = $this->input->post('name', true);
        $id = $this->input->post('id', true);
        $active = $this->input->post('active', true);
        $shortDes = $this->input->post('shortDes', true);
        $des = htmlspecialchars($this->input->post('des'), ENT_QUOTES);
        $query = $this->admin_category_model->insertCateNews($id, $name, $shortDes, $des, $active, $idUser);
        echo $query;
    }

    public function getInfoCateNewsID()
    {
        $id = $this->input->post('id');
        $result= $this->admin_category_model->getInfoCateNewsID($id);
        $data = $result[name]."?=".$result[short_description]."?=".htmlspecialchars_decode($result[description])."?=".$result[parentid]."?=".$result[active];
        echo $data;
    }

    public function updateCategoryNews()
    {
        $id = $this->input->post('id', true);
        $name = $this->input->post('name', true);
        $shortDes = $this->input->post('shortDes', true);
        $des = htmlspecialchars($this->input->post('des'), ENT_QUOTES);
        $parentid = $this->input->post('parentid', true);
        $active = $this->input->post('active', true);

        $query = $this->admin_category_model->updateCategoryNews($id, $name, $shortDes, $des, $parentid, $active);
        echo $query;
    }

    public function getCateNewsParentID()
    {
        $field = "count(id)";
        $id = $this->input->post('id');
        $query = $this->admin_category_model->getCateNewsParentID($field, $id);
        echo $query;
    }

    //ham xoa danh muc
    public function deleteCategory($id){
        $query = $this->admin_category_model->deleteCategoryNews($id); //check thao tac cuoi cung (thao tac xoa)
        $subCate = $this->admin_category_model->getParentCategory($id);
        if(!empty($subCate)){
            foreach($subCate as $objSubCate){
                $this->deleteCategory($objSubCate->id);
            }
        }
        $replacedID = $this->getParentID($id);
        $replacedName = $this->getParentName($replacedID);
        $this->moveThreads($id, $replacedID);
        echo $replacedName." - ";
    }

    //ham lay danh muc de chuyen san pham da tao
    public function getParentID($id){
        $info1 = $this->admin_category_model->getInfoCateNewsID($id);
        $parentID = $info1[parentid];
        if($parentID == 0){
            $info2 = $this->admin_category_model->getNewestCategory();
            $parentID = $info2[id];
            if(empty($info2)){
                $parentID = 1;
            }
        }
        return $parentID;
    }

    //ham lay ten danh muc
    public function getParentName($id){
        $info2 = $this->admin_category_model->getInfoCateNewsID($id);
        $name = $info2[name];
        return $name;
    }

    public function moveThreads($id, $newID){
        $query = $this->admin_category_model->moveThreads($id, $newID);
        return $query;
    }

    public function sessionTest()
    {
        echo $this->session->userdata('categoryNameMove');
    }

    public function getIDUser(){
        $email = $this->session->userdata('email');
        $field = 'id';
        $idUser = $this->users_model->getInfoUser($field, $email);
        return $idUser;
    }

    public function getSubCategoryNewsEdit()
    {
        $idCateNews = $this->input->post('id');
        $data['info'] = $this->admin_category_model->getSubCategoryNews($idCateNews);
        $this->load->view('admin/category/parent_category', $data);
    }

    public function getParentCategory($id)
    {
//        echo $id;
        $data['info'] = $this->admin_category_model->getParentCategory($id);
        $data['idBefore'] = $id;
        $this->load->view('admin/category/parent_category', $data);
    }

    public function getEditParentCategory($id)
    {
//        echo $id;
        $data['info'] = $this->admin_category_model->getParentCategory($id);
        $data['idBefore'] = $id;
        $this->load->view('admin/category/edit_parent_category', $data);
    }

    public function getAddParentCategory($id)
    {
//        echo $id;
        $data['info'] = $this->admin_category_model->getParentCategory($id);
        $data['idBefore'] = $id;
        $this->load->view('admin/category/add_parent_category', $data);
    }
}